/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FrameWork.Objects.Character;

import FrameWork.GameScreen;
import FrameWork.Objects.MyObject;
import FrameWork.RSMainGame;
import FrameWork.Rectangle;
import GamePlay.Global;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author ThanhTri
 */
public class MyCharacter extends MyObject{

    protected int   m_NextX,m_NextY;
    protected float m_Veloc;  
    protected float   m_CurX,m_CurY;
    protected int   m_Count;
    protected int   m_Direct;
    protected int   m_CurDirect;
    
    
    protected  boolean m_iRun;
    
    public void setNextPostion (int _NX,int _NY){
        m_NextX = _NX;
        m_NextY = _NY;
    }
    public void setDirection (int _Dir){
        m_Direct =_Dir;
    }
   
    public void setRun (boolean _iRun){
        m_iRun =  _iRun;
    }   
    public float getCurX() {
        return m_CurX;
    }

    public float getCurY() {
        return m_CurY;
    }
    
    public int getDirection() {
        return m_CurDirect;
    }
     public Rectangle getRect (){
        return new Rectangle((int)m_CurX ,(int) m_CurY , m_SizeX, m_SizeY);
    }
    public int ReverseDirect (){
        int _Dir  = 0;
        switch(m_Direct ) {
            case Global.DIR_DOWN:
                _Dir = Global.DIR_UP;
                break;
            case Global.DIR_UP:
                _Dir = Global.DIR_DOWN;
                break;
            case Global.DIR_LEFT:
                _Dir = Global.DIR_RIGHT;
                break;
            case Global.DIR_RIGHT:
                _Dir = Global.DIR_LEFT;
                break;
        }
        return  _Dir;
    }
          
    public MyCharacter(int _X,int _Y, int _SizeX, int _SizeY,
            float _TimeAni, float _Veloc,int _Direct,
            int _Map[][],int _Width, int _Height,
            int _ID){     
        super(_X, _Y, _SizeX, _SizeY,
                _TimeAni, _Map, _Width, _Height, _ID);
        m_CurX          = _X*30;
        m_CurY          = _Y*30;
        m_Veloc         = _Veloc;
        m_CurDirect     = m_Direct = _Direct;
        
        m_Count         = 0;
        m_iRun          = false;
        m_Sprite        = RSMainGame.getRS().getElefire();
       
    }
   public void UpdateMove (float _Time,int _Terrain[][]){
          
   }
  
    public void MoveLayout (float _Time){
       int dX,dY;
       dX= dY =0;
       if(m_X!= m_NextX){
           dX = m_NextX-m_X;
       }
       if(m_Y!= m_NextY){
           dY = m_NextY-m_Y;
       }
       
        m_CurX+= dX*m_Veloc*_Time;
        m_CurY+= dY*m_Veloc*_Time;
       
       if (dX<0){
           if((m_CurX - m_NextX *30)<0){
               m_X = m_NextX;
               m_CurX = m_X*m_SizeX;
               m_iRun = false;
               m_CurDirect = m_Direct;
           }
       }else{
           if (dX>0){
               if((m_CurX - m_NextX *30)>0){
                   m_X = m_NextX;
                   m_CurX = m_X*m_SizeX;
                   m_iRun = false;
                   m_CurDirect = m_Direct;
               }
           }
       }
       
       
       if (dY<0){
           if((m_CurY - m_NextY *30)<0){
               m_Y = m_NextY;
               m_CurY = m_Y*m_SizeY;
               m_iRun = false;
               m_CurDirect = m_Direct;
           }
       }else{
           if (dY>0){
               if((m_CurY - m_NextY *30)>0){
                   m_Y = m_NextY;
                   m_CurY = m_Y*m_SizeY;
                   m_iRun = false;
                   m_CurDirect = m_Direct;
               }
           }
       }
       
    }
    public void FindNextStep (int _X,int _Y,int _Direct){
        
        switch(_Direct){
            case Global.DIR_LEFT:
                if(_X >= 1){
                    m_NextX = _X-1;
                }
                break;
            case Global.DIR_RIGHT:
                if(_X <m_WidthMap-1){
                    m_NextX = _X+1;
                }
                break;
            case Global.DIR_UP:
                if(_Y>=1) {
                    m_NextY = _Y-1;
                }
                break;
            case Global.DIR_DOWN:
                if(_Y < m_HeightMap-1){
                    m_NextY = _Y+1;
                }
                break;
        }
    }
    public void UpdateAnimation (float _Time){
    }
    public void DrawScreen (Graphics _Graphics,GameScreen _Screen){
        m_Sprite.setFrame(m_CurFrame);
        m_Sprite.setPosition((int)m_CurX - (_Screen.getXView() - _Screen.getWidth()/2), 
                (int)m_CurY - (_Screen.getYView() - _Screen.getHeight()/2));
        
        m_Sprite.paint(_Graphics);
    }
}
